//
//  SelfHeaderView.m
//  JiaoX
//
//  Created by 尹桥印 on 14/11/19.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "SelfHeaderView.h"

#import "CalcView.h"

#define HeaderItemHeight [DimensUtils getScaleX:50]

@interface SelfHeaderView()<SelfHeaderItemViewDelegate>
@property (nonatomic,strong)UIImageView *image;
@property(nonatomic,strong) SelfHeaderItemView *header;
@end
@implementation SelfHeaderView


-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self!=nil){
        //设置自己的bounds
        CGFloat selfHeight = JXScreenW/3.02;
        self.bounds = CGRectMake(0, 0, JXScreenW, selfHeight+HeaderItemHeight+25);
        //用户头像or登陆的btn
        UIButton *loginBtn = [self getLoginRegisterButtonWithTitle:@"登录"];
        CGFloat btnWidth = [DimensUtils getScaleX:62];
        CGFloat btnHeight = btnWidth;
        CGFloat loginX = (JXScreenW*0.5-btnWidth*0.5);
        CGFloat loginY = (selfHeight-btnHeight)*0.5;
        JXLog(@"loginY=%f",loginY);
        loginBtn.frame = CGRectMake(loginX,loginY, btnWidth, btnHeight);
        self.loginBtn = loginBtn;
        [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:loginBtn];
        
        //显示用户名称的label
        UILabel *nameLabel = [self getLoginWithName:@""];
        [nameLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
        CGSize titleSize = [nameLabel.text sizeWithFont:[nameLabel font]];
        nameLabel.frame = CGRectMake(JXScreenW/2-titleSize.width/2, CGRectGetMaxY(loginBtn.frame), titleSize.width, titleSize.height);
        self.nameLabel = nameLabel;
        [self addSubview:nameLabel];
        
        //背景图片
        UIImageView *imageBg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, JXScreenW, selfHeight)];
        imageBg.image = [UIImage imageNamed:@"self_header_bg"];
        [self insertSubview:imageBg atIndex:0];
        
        self.image = [[UIImageView alloc]init];
        [self.image setHidden:YES];
        [self addSubview:self.image];
        
        //增加订单，收藏，代金卷等信息 ,TODO 判断点击，设置数据
        _header  = [[SelfHeaderItemView alloc]init];
        _header.x = 0;
        _header.y = selfHeight + JXPublicInset;
        _header.delegate = self;
        [self addSubview:_header];
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(UIButton *)getLoginRegisterButtonWithTitle:(NSString *)title{
    //添加登录的btn
    UIButton *loginBtn = [[UIButton alloc]init];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:19];
    loginBtn.titleLabel.textColor = [UIColor whiteColor];
    loginBtn.backgroundColor = [UIColor clearColor];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"self_header_login_pre"] forState:UIControlStateNormal];
    [loginBtn setTitle:title forState:UIControlStateNormal];
    [loginBtn setTitleColor:JXThemeColor forState:UIControlStateHighlighted];
    [loginBtn setBackgroundImage:[UIImage imageNamed:@"self_header_login_nor"] forState:UIControlStateHighlighted];
    return loginBtn;
}

-(UILabel *)getLoginWithName:(NSString *)title{
    //添加显示昵称的label
    UILabel *nameLabel = [[UILabel alloc]init];
    [nameLabel setText:title];
    [nameLabel setTextColor:[UIColor whiteColor]];
    [nameLabel setFont:[UIFont systemFontOfSize:15]];
    return nameLabel;
}

/**
 *点击登录按钮
 */
-(void)loginBtnClick{
    if ([self.delegate respondsToSelector:@selector(loginBtnClick)]) {
          [_delegate loginBtnClick];
     }
}
-(void)setVip:(BOOL)isVip {
    if (isVip) {
        [self.image setHidden:NO];
        NSString *genderImg = @"ic_gender_unknow_vip";
        //判断是男是女
        if ([[UserDefaultsHelper stringForKey:KEY_USER_GENDER] isEqualToString:@"1"]) {
            genderImg = @"ic_gender_man_vip";
        }else if([[UserDefaultsHelper stringForKey:KEY_USER_GENDER] isEqualToString:@"2"]){
            genderImg = @"ic_gender_woman_vip";
        }
        //调整位置
        [self.image setImage:[UIImage imageNamed:genderImg]];
        [self.image setSize:self.image.image.size];
        [self.image setCenterY:self.nameLabel.centerY];
        [self.nameLabel setX:self.centerX-self.image.width/2-self.nameLabel.width/2];
        [self.image setX:CGRectGetMaxX(self.nameLabel.frame)+2.5];
    }else{
        [self.image setHidden:YES];
        [self.nameLabel setCenterX:self.centerX];
    }
}

-(void)adjustLocation:(BOOL)isLogin{
    if (isLogin) {
        [self.loginBtn setY:self.centerY-self.nameLabel.font.lineHeight/2-15-self.loginBtn.height/2-HeaderItemHeight/2];
        [self.nameLabel setY:CGRectGetMaxY(self.loginBtn.frame)+2];
    }else{
        [self.loginBtn setCenterY:self.centerY-15-HeaderItemHeight/2];
        [self.image setHidden:YES];
    }
}

-(void)setOrderNum:(NSInteger)orderNum{
    [_header setBadgeNum:orderNum];
}

-(void)clickItemOrder{
    [_delegate clickOrder];
}

-(void)clickItemVolume{
    [_delegate clickVolume];
}

-(void)clickItemMark{
    [_delegate clickMark];
}

@end
